package com.efast.dmom.mgmt.service.les.impl;

import java.util.*;

import com.efast.dmom.mgmt.dao.les.TwBasPackSkuQtyDao;
import com.efast.dmom.mgmt.entity.les.TwBasPackSkuQty;
import com.efast.dmom.mgmt.entity.mes.TeMesOutbound;
import com.efast.dmom.mgmt.service.les.TwBasPackSkuQtyService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("twBasPackSkuQtyServiceImpl")
public class TwBasPackSkuQtyServiceImpl implements TwBasPackSkuQtyService {
	@Autowired
	private TwBasPackSkuQtyDao twBasPackSkuQtyDao;

	@Override
	public long getTwBasPackSkuQtyRowCount(Assist assist) {
		return twBasPackSkuQtyDao.getTwBasPackSkuQtyRowCount(assist);
	}

	@Override
	public List<TwBasPackSkuQty> selectTwBasPackSkuQty(Assist assist) {
		return twBasPackSkuQtyDao.selectTwBasPackSkuQty(assist);
	}

	@Override
	public TwBasPackSkuQty selectTwBasPackSkuQtyByObj(TwBasPackSkuQty obj) {
		return twBasPackSkuQtyDao.selectTwBasPackSkuQtyByObj(obj);
	}

	@Override
	public TwBasPackSkuQty selectTwBasPackSkuQtyById(Integer id) {
		return twBasPackSkuQtyDao.selectTwBasPackSkuQtyById(id);
	}

	@Override
	public int insertTwBasPackSkuQty(TwBasPackSkuQty value) {
		return twBasPackSkuQtyDao.insertTwBasPackSkuQty(value);
	}

	@Override
	public int insertNonEmptyTwBasPackSkuQty(TwBasPackSkuQty value) {
		return twBasPackSkuQtyDao.insertNonEmptyTwBasPackSkuQty(value);
	}

	@Override
	public int insertTwBasPackSkuQtyByBatch(List<TwBasPackSkuQty> value) {
		return twBasPackSkuQtyDao.insertTwBasPackSkuQtyByBatch(value);
	}

	@Override
	public int deleteTwBasPackSkuQtyById(Integer id) {
		return twBasPackSkuQtyDao.deleteTwBasPackSkuQtyById(id);
	}

	@Override
	public int deleteTwBasPackSkuQty(Assist assist) {
		return twBasPackSkuQtyDao.deleteTwBasPackSkuQty(assist);
	}

	@Override
	public int updateTwBasPackSkuQtyById(TwBasPackSkuQty enti) {
		return twBasPackSkuQtyDao.updateTwBasPackSkuQtyById(enti);
	}

	@Override
	public int updateTwBasPackSkuQty(TwBasPackSkuQty value, Assist assist) {
		return twBasPackSkuQtyDao.updateTwBasPackSkuQty(value, assist);
	}

	@Override
	public int updateNonEmptyTwBasPackSkuQtyById(TwBasPackSkuQty enti) {
		return twBasPackSkuQtyDao.updateNonEmptyTwBasPackSkuQtyById(enti);
	}

	@Override
	public int updateNonEmptyTwBasPackSkuQty(TwBasPackSkuQty value, Assist assist) {
		return twBasPackSkuQtyDao.updateNonEmptyTwBasPackSkuQty(value, assist);
	}

	@Override
	public void updateTwBasPackSkuQtyByProduce(List<TeMesOutbound> outboundList) {
		//查询是否存在
		List<TwBasPackSkuQty> twBasPackSkuQtyList = twBasPackSkuQtyDao.queryTwBasPackSkuExist(outboundList);
		Map<String,TwBasPackSkuQty> map=new HashMap<>();
		if(twBasPackSkuQtyList!=null&&twBasPackSkuQtyList.size()>0){
			for(TwBasPackSkuQty t:twBasPackSkuQtyList){
                 map.put(t.getPartNoVersion()+"_"+t.getLocNo(),t);
			}
		}
		List<TwBasPackSkuQty> addValue=new ArrayList<>();

		List<TwBasPackSkuQty> editValue=new ArrayList<>();
		for(int i=0;i<outboundList.size();i++) {
			  TwBasPackSkuQty t=map.get(outboundList.get(i).getPartNoVersion()+"_"+outboundList.get(i).getErrMsg());
              if(t!=null){
              	t.setStorageQty(t.getStorageQty()+outboundList.get(i).getPartQty());
				  editValue.add(t);
			  }else{
              	  t=new TwBasPackSkuQty();
              	  t.setPartNoVersion(outboundList.get(i).getPartNoVersion());
              	  t.setLocNo(outboundList.get(i).getErrMsg());
              	  t.setStorageQty(outboundList.get(i).getPartQty());
              	  t.setMriStatus("0");
              	  t.setCreateTime(new Date());
              	  t.setLastUpdateTime(new Date());
				  addValue.add(t);
			  }

		}
        if(editValue.size()>0){
			twBasPackSkuQtyDao.editTwBasPackSkuQtyByExist(editValue);
		}
		if(addValue.size()>0){
			twBasPackSkuQtyDao.insertTwBasPackSkuQtyByBatch(addValue);
		}
	}

	public TwBasPackSkuQtyDao getTwBasPackSkuQtyDao() {
		return this.twBasPackSkuQtyDao;
	}

	public void setTwBasPackSkuQtyDao(TwBasPackSkuQtyDao twBasPackSkuQtyDao) {
		this.twBasPackSkuQtyDao = twBasPackSkuQtyDao;
	}




}